/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ai;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.LinkedList;

/**
 *
 * @author mammad
 */
public class KeyWordAI {

    private QueryForm f;
    private Jdbc jdbc;
    private LinkedList<String> ingr;
    private LinkedList<String> mealCat;
    private Connection mCon;
    ArrayList<String> incIngr;
    ArrayList<String> excIngr;
    ArrayList<String> incMealcat;
    ArrayList<String> excMealcat;

    public KeyWordAI(QueryForm form, Connection conn) {

        f = form;
        jdbc = new Jdbc(f);
        jdbc.getDatabaseIngr(conn);
        jdbc.getDatabaseMealCat(conn);
        ingr = f.getDatabaseIngr();
        incIngr = new ArrayList<String>();
        excIngr = new ArrayList<String>();
        incMealcat = new ArrayList<String>();
        excMealcat = new ArrayList<String>();

        mealCat = f.getDatabaseMealCat();
        mCon = conn;
        //  ingr = new LinkedList<String>();
        mealCat = new LinkedList<String>();
    //  ingr.add("banana");
    // ingr.add("apple");
    //ingr.add("pepper");
    //ingr.add("meat");


    }

    public void collectUserIncIngr(ArrayList<String> userInput) {

        ArrayList<String> words = userInput;
        String s;
        if (words.contains("or")) {
            s = "So you want a meal whith either ";
            for (int j = 0; j < words.size(); j++) {
                for (int k = 0; k < ingr.size(); k++) {
                    if (words.get(j).equals(ingr.get(k))) {
                        System.out.println("incORingr " + words.get(j));
                        f.addIncIngredient(words.get(j));

                        s += words.get(j);
                        if (j != words.size() - 1) {
                            s += " or ";
                        }
                    }
                }
            }
        // jdbc.checkOrIncIngrediens(mCon);
        //System.out.println(s);
        //   System.out.println("Let me see what i can Find");

        } else if (words.contains("and") || words.contains("both")) {
            s = "So you want a meal what include both ";

            for (int j = 0; j < words.size(); j++) {
                for (int k = 0; k < ingr.size(); k++) {
                    if (words.get(j).equals(ingr.get(k))) {
                        //             System.out.println("incAndingr " + words.get(j));
                        f.addIncIngredient(words.get(j));

                        s += words.get(j);
                        if (j != words.size() - 1) {
                            s += " and ";
                        }
                    }
                }
            }
        //   jdbc.checkAndIncIngrediens(mCon);
        // System.out.println(s);
        //System.out.println("Let me see what i can Find");

        } else {
            s = "So you like ";
            for (int j = 0; j < words.size(); j++) {
                for (int k = 0; k < ingr.size(); k++) {
                    if (words.get(j).equals(ingr.get(k))) {
                        //            System.out.println("incingr " + words.get(j));
                        s += words.get(j);
                        f.addIncIngredient(words.get(j));
                    //   jdbc.checkAndIncIngrediens(mCon);

                    }
                }
            }



        }
        if (!f.getIncIngrediens().isEmpty()) {
            jdbc.checkAndIncIngrediens(mCon);
            System.out.println(s);
            System.out.println("Let me see what i can Find");
        }
    }

    public void collectUserExcIngr(ArrayList<String> userInput) {

        ArrayList<String> words = userInput;
        String s;
        if (words.contains("or")) {
            s = "So you like neither ";
            for (int j = 0; j < words.size(); j++) {
                for (int k = 0; k < ingr.size(); k++) {
                    if (words.get(j).equals(ingr.get(k))) {
                        //          System.out.println("ExcORingr " + words.get(j));
                        s += words.get(j);
                        if (j != words.size() - 1) {
                            s += " or ";
                        }
                        f.addExcIngredient(words.get(j));

                    }
                }
            }

        //System.out.println(s);
        //System.out.println("Let me see what i can Find");

        } else if (words.contains("and")) {
            System.out.println("jag är här och mårbra");
            s = "So you dont like ";
            for (int j = 0; j < words.size(); j++) {
                for (int k = 0; k < ingr.size(); k++) {
                    if (words.get(j).equals(ingr.get(k))) {
                        System.out.println("EXcAndingr " + words.get(j));

                        s += words.get(j);
                        if (j != words.size() - 1) {
                            s += " and ";
                        }
                        f.addExcIngredient(words.get(j));


                    }
                }
            }

        //System.out.println(s);
        //System.out.println("Let me see what i can Find");

        } else {
            s = "So you dont like ";
            for (int j = 0; j < words.size(); j++) {
                for (int k = 0; k < ingr.size(); k++) {
                    if (words.get(j).equals(ingr.get(k))) {
                        System.out.println("Excingr " + words.get(j));
                        s += words.get(j);
                        f.addExcIngredient(words.get(j));
                    //            jdbc.checkAndExcIngrediens(mCon);
                    }
                }
            }



        }
        if (!f.getExcIngrediens().isEmpty()) {
            System.out.println(s);
            System.out.println("Let me see what i can Find");
            jdbc.checkAndExcIngrediens(mCon);
        }


    }

    public void collectUserExcMealCat(ArrayList<String> userInput) {
        ArrayList<String> words = userInput;
        for (int j = 0; j < words.size(); j++) {
            for (int k = 0; k < mealCat.size(); k++) {
                if (words.get(j).equals(mealCat.get(k))) {
                    System.out.println("mealCat " + words.get(j));
                }
                f.addMealCategory(words.get(j));
                jdbc.checkExcMealCategories(mCon);
            }
        }
    }

    public void collectUserIncMealCat(ArrayList<String> userInput) {
        ArrayList<String> words = userInput;
        for (int j = 0; j < words.size(); j++) {
            for (int k = 0; k < mealCat.size(); k++) {
                if (words.get(j).equals(mealCat.get(k))) {
                    System.out.println("incORingr " + words.get(j));
                    f.addMealCategory(words.get(j));
                    jdbc.checkIncMealCategories(mCon);

                }
            }
        }
    }

    public void inputHandler(String userInput) {


        String[] w = userInput.split(" ");
        boolean m = true;
        int k = 0;
        while (m && k < w.length) {
            // System.out.println(w.length);
            if (!w[k].equals("not")) {

                // System.out.println(k);
                //    System.out.println("inc"+w[k]);
                incIngr.add(w[k]);


            } else {
                m = false;
            }
            k += 1;
        }




        int l = 0;

        for (int i = 0; i < w.length; i++) {
            if (w[i].equals("not")) {
                l = i;

                while (l < w.length) {
                    //  System.out.println("exc"+w[l]);
                    excIngr.add(w[l]);

                    //}

                    l++;
                }
            }

        }






        if (!incIngr.isEmpty()) {
            collectUserIncIngr(incIngr);
        }
        if (!excIngr.isEmpty()) {
            collectUserExcIngr(excIngr);
        }
        /*    if (!incIngr.isEmpty()&& !excIngr.isEmpty()) {
        System.out.println("jag är här och mår bra");
        jdbc.checkAndIncAndExcIngrediens(mCon);
        }*/
        if (!incMealcat.isEmpty()) {
            collectUserIncMealCat(incMealcat);
        }
        if (!excMealcat.isEmpty()) {
            collectUserExcMealCat(excMealcat);
        }

    }

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    /**
     *
     * @author root
     */
    /**
     * @param args the command line arguments
     */
    /*  public static void main(String[] args) throws IOException {
    //  KeyWordAI key = new KeyWordAI();
    QueryForm lQF = null;
    Jdbc j = null;
    Connection conn = null;
    lQF = new QueryForm(conn);
    j = new Jdbc(lQF);
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("What can I do for you?");
    String mode = "";
    System.out.print("> ");
    mode = input.readLine();
    while (!(mode.equals("no"))) {
    System.out.print("Is there anything else you whould like?\n");
    System.out.print("> ");
    mode = input.readLine();
    }
    j.finalMeal(conn);
    }*/
}
    
        
    

   




